package com.asset.salesforce.dao;
///**
// * 
// */
//package com.asset.salesforce.domain;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import org.apache.lucene.search.Sort;
//import org.apache.lucene.search.SortField;
//import org.hibernate.Query;
//import org.hibernate.Session;
//import org.hibernate.SessionFactory;
//import org.hibernate.search.FullTextSession;
//import org.hibernate.search.Search;
//import org.hibernate.search.query.dsl.QueryBuilder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Repository;
//
//import com.asset.salesforce.dto.SearchResults;
//
///**
// * @author Hisham
// *
// */
//@Repository
//public class SearchDaoImpl {
//
//	@Autowired
//    private SessionFactory sessionFactory;
//
//	 @SuppressWarnings("unchecked")
//	public void index() {
//         Session session =  sessionFactory.getCurrentSession();
//         FullTextSession fullTextSession = Search.getFullTextSession(session);
//         List<Company>companies =  session.createQuery("from Company as company").list();
//         for (Company company : companies) {
//        	 fullTextSession.index(company);
//         }
//         List<Employee>employees = session.createQuery("from Employee as employee").list();
//         for (Employee employee : employees) {
//        	 fullTextSession.index(employee);
//		}
//         List<Lead> leads = session.createQuery("from Lead as lead").list();
//         for (Lead lead : leads) {
//         fullTextSession.index(lead);
//         }
//         fullTextSession.flushToIndexes();
//         System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<INdexing sucess");
// }
//	 
//	 @SuppressWarnings("unchecked")
//	public List<Lead> getAllLeadByCompany(SearchResults search,Integer companyId,Integer index,Integer count,String column,String sortDir) {
//			Session session =sessionFactory.getCurrentSession();
//			List<Lead> leads = new ArrayList<Lead>();
//			if(search!=null){
//	            FullTextSession fullTextSession = Search.getFullTextSession(session);
//	            String [] fields = new String[]{"fName","lName","phone","mobile","companyName","leadOwner.fName"};
//	            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lead.class).get();
//	            
//	            org.apache.lucene.search.Query query =  builder.bool()  
//	            .must(builder  
//	                    .bool()  
//	                    .should(builder.keyword()  
//	                            .onFields(fields)  
//	                            .matching(search.getLeadStatus()+"*").createQuery()).createQuery())  
//	            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
//	            .must(builder.keyword().onField("isConverted").matching(false).createQuery())
//	            .must(builder.keyword().onField("leadOwner.company.id").matching(companyId).createQuery())
//	            .createQuery();
//	            
//	            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Lead.class).setFirstResult(index).setMaxResults(count);
//	            boolean dir = false;
//	            if(sortDir.equals("desc")){
//	            	dir = true;
//	            }
//	            org.apache.lucene.search.Sort sort = new Sort(new SortField(""+column+"", SortField.STRING,dir));
//	            hibernateuery.setSort(sort);
//	            leads = hibernateuery.list();
//				System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<lead size>>>>>>>>>>>>>>>"+leads.size());
//			}
//			return  leads;
//		}
//		
//		@SuppressWarnings("unchecked")
//		public List<Lead> getAllLeadByEmployee(SearchResults search,Integer empId,Integer index,Integer count,String column,String sortDir) {
//			Session session =sessionFactory.getCurrentSession();
//			List<Lead> leads = new ArrayList<Lead>();
//			if(search!=null){
//				FullTextSession fullTextSession = Search.getFullTextSession(session);
//	            String [] fields = new String[]{"fName","lName","phone","mobile","companyName","leadOwner.fName"};
//	            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lead.class).get();
//	            
//	            org.apache.lucene.search.Query query =  builder.bool()  
//	            .must(builder  
//	                    .bool()  
//	                    .should(builder.keyword()  
//	                            .onFields(fields)  
//	                            .matching(search.getLeadStatus()+"*").createQuery()).createQuery())  
//	            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
//	            .must(builder.keyword().onField("isConverted").matching(false).createQuery())
//	            .must(builder.keyword().onField("leadOwner.id").matching(empId).createQuery())
//	            .createQuery();
//	            
//	            org.hibernate.search.FullTextQuery hibernateuery = fullTextSession.createFullTextQuery(query, Lead.class).setFirstResult(index).setMaxResults(count);
//	            boolean dir = false;
//	            if(sortDir.equals("desc")){
//	            	dir = true;
//	            }
//	            org.apache.lucene.search.Sort sort = new Sort(new SortField(""+column+"", SortField.STRING,dir));
//	            hibernateuery.setSort(sort);
//				leads = hibernateuery.list();
//				System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<lead size>>>>>>>>>>>>>>>"+leads.size());
//			}
//			return  leads;
//		}
//		
//		@SuppressWarnings("unchecked")
//		public Integer getAllSizeForOwner(SearchResults search,String companyName) {
//			Session session =sessionFactory.getCurrentSession();
//			List<Lead> leads = new ArrayList<Lead>();
//			if(search!=null){
//	            FullTextSession fullTextSession = Search.getFullTextSession(session);
//	            String [] fields = new String[]{"fName","lName","phone","mobile","companyName","leadOwner.fName"};
//	            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lead.class).get();
//	            
//	            org.apache.lucene.search.Query query =  builder.bool()  
//	            .must(builder  
//	                    .bool()  
//	                    .should(builder.keyword()  
//	                            .onFields(fields)  
//	                            .matching(search.getLeadStatus()+"*").createQuery()).createQuery())  
//	            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
//	            .must(builder.keyword().onField("isConverted").matching(false).createQuery())
//	            .must(builder.keyword().onField("leadOwner.company.companyName").matching(companyName).createQuery())
//	            .createQuery();
//	            
//	            Query hibernateuery = fullTextSession.createFullTextQuery(query, Lead.class);
//				leads = hibernateuery.list();
//				System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<lead size>>>>>>>>>>>>>>>"+leads.size());
//			}		
//			if(leads.size()>0){
//				return  leads.size();
//			}
//			else{
//				return 0;
//			}
//		}
//		
//		@SuppressWarnings("unchecked")
//		public Integer getAllSizeForEmployee(SearchResults search,Integer empId) {
//			Session session = sessionFactory.getCurrentSession();
//			List<Lead> leads = new ArrayList<Lead>();
//			if(search!=null){
//				FullTextSession fullTextSession = Search.getFullTextSession(session);
//	            String [] fields = new String[]{"fName","lName","phone","mobile","companyName","leadOwner.fName"};
//	            QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Lead.class).get();
//	            
//	            org.apache.lucene.search.Query query =  builder.bool()  
//	            .must(builder  
//	                    .bool()  
//	                    .should(builder.keyword()  
//	                            .onFields(fields)  
//	                            .matching(search.getLeadStatus()+"*").createQuery()).createQuery())  
//	            .must(builder.keyword().onField("isDeleted").matching(false).createQuery())  
//	            .must(builder.keyword().onField("isConverted").matching(false).createQuery())
//	            .must(builder.keyword().onField("leadOwner.id").matching(empId).createQuery())
//	            .createQuery();
//	            
//	            Query hibernateuery = fullTextSession.createFullTextQuery(query, Lead.class);
//				leads = hibernateuery.list();
//				System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<lead size>>>>>>>>>>>>>>>"+leads.size());
//			}
//			if(leads.size()>0){
//				return  leads.size();
//			}
//			else{
//				return 0;
//			}
//		}
//
//}
